
import cloud from '@lafjs/cloud'
import nw from "nw-lafjs";
import {ok} from "@/utils/responseUtil";
const db = cloud.database()
const _ = db.command
export default async function (ctx: FunctionContext) {
  let body = ctx.body
  /**
   * {
   *   "page": 1,
   *   "limit": 10,
   *   "roleName": "wuzuxian"
   * }
   */
  //参数校验
  if (nw.util.isNull(body.page)){
    body.page = 1
  }
  if (nw.util.isNull(body.limit)){
    body.limit = 10
  }
  //2.组装查询条件
  let whereJson = {deleted:0}
  if (!nw.util.isNull(body.roleName)){
    // @ts-ignore
    whereJson.roleName = _.eq(body.roleName)
  }
  //3.分页查询即可
  let data = await nw.db.select({
    dbName:"role",
    whereJson:whereJson,
    getCount:true,
    pageIndex:body.page,
    pageSize:body.limit,
    sortArr:[{ "name": "createTime","type": "desc" }]
  })
  //4.组装数据结构即可
  let result =  {
    "records": [],
    "total": 0,
    "size": body.limit,
    "current": 1,
    "pages": 0
  }
  result.records = data.rows.map(val=>{
    return nw.util.deleteObjectKeys(val,["phone","wareId","deleted","token","loginTime"])
  })
  result.total = data.count
  result.current = body.page
  result.size = body.limit
  result.pages = Math.ceil(result.total / result.size)
  return ok(result,"查询成功")
}
